//
//  HomeViewController.m
//  QuicklyPlay
//
//  Created by 你的温柔是我唯一沉溺 on 15/10/30.
//  Copyright © 2015年 OPteam. All rights reserved.
//

#import "AppDelegate.h"
#import "HomeViewController.h"
#import "AlbumCell.h"
#import "HotsCell.h"


#import "HotsViewController.h"
#import "SortViewController.h"

@interface HomeViewController()

{
    HotsViewController *hotsVC;
    SortViewController *sortVC;
}


//标签选择器属性
@property (weak, nonatomic) IBOutlet UISegmentedControl *homeSegment;
@property (weak, nonatomic) IBOutlet UIView *homeView;
@property (weak, nonatomic) IBOutlet UIButton *refreshButton;


@property (strong, nonatomic) NetworkHelper *network;
@property (strong, nonatomic) UIViewController *currentVC;



@end

@implementation HomeViewController


-(NetworkHelper *)network {
    if (!_network) {
        _network = [NetworkHelper sharedInstance];
    }
    return _network;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupChildViewControllers];
    
    [self.homeSegment addTarget:self action:@selector(homeSegmentDidClicked:) forControlEvents:UIControlEventValueChanged];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}



-(void) setupChildViewControllers {
    
    hotsVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HotsVC"];
    sortVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"SortVC"];

    [self addChildViewController:hotsVC];
    [self addChildViewController:sortVC];

    if (K_Width == 375 && K_Height == 667) { //6s 6
         hotsVC.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 135 , [UIScreen mainScreen].bounds.size.height - 530);
    }else if(K_Width == 414 && K_Height == 736){ //plus
          hotsVC.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 173 , [UIScreen mainScreen].bounds.size.height - 600);
    }else if(K_Width == 320 && K_Height == 568){ //5s 5
        hotsVC.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 80 , [UIScreen mainScreen].bounds.size.height - 430);
    }else if(K_Width == 320 && K_Height == 480){ //4s 4
        hotsVC.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 80 , [UIScreen mainScreen].bounds.size.height - 345);
    }


    
   
    [self.homeView addSubview: hotsVC.view];
    //NSLog(@"%@", hotsVC.view);
    self.currentVC = hotsVC;
    
}





//标签选择器点击事件
- (IBAction)homeSegmentDidClicked:(UISegmentedControl *)sender {

    
   
    if(self.currentVC==hotsVC && sender.selectedSegmentIndex == 0) {
        return;
    }
    
    if(self.currentVC==sortVC && sender.selectedSegmentIndex ==1) {
        return;
    }
    
    
    UIViewController *oldVC = self.currentVC;
    
    
    
    switch (sender.selectedSegmentIndex) {
        case 0:
        {
            self.view.userInteractionEnabled = NO;
            self.refreshButton.hidden = NO;
            [self transitionFromViewController:self.currentVC toViewController:hotsVC duration:0 options:UIViewAnimationOptionTransitionNone animations:^{
                
            } completion:^(BOOL finished) {
                if (finished) {
                    self.currentVC = hotsVC;

                } else {
                    self.currentVC = oldVC;
                }

                self.view.userInteractionEnabled = YES;
                
            }];
            break;
        }
            
            
        case 1:
        {
            self.refreshButton.hidden = YES;
            self.view.userInteractionEnabled = NO;
            [self transitionFromViewController:self.currentVC toViewController:sortVC duration:0 options:UIViewAnimationOptionTransitionNone animations:^{
                
            } completion:^(BOOL finished) {
                if (finished) {
                    self.currentVC = sortVC;

                } else {
                    self.currentVC = oldVC;
                }
                self.view.userInteractionEnabled = YES;
            }];
            break;
        }
        
        default:
            break;
    }
}


//抽屉
- (IBAction)OpenMenuList:(UIButton *)sender {
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (tempAppDelegate.leftSlideVC.closed)
    {
        [tempAppDelegate.leftSlideVC openLeftView];
    }
    else
    {
        [tempAppDelegate.leftSlideVC closeLeftView];
    }
}


- (IBAction)refreshBtnDidClicked:(UIButton *)sender {
    
    [self.network downloadWithURL:k_HotsURL WithRequestAllDataDidFinish:^(BOOL status) {
        if (status) {
             [hotsVC.hostTableView reloadData];
        } else {
            NSLog(@"没网络 (HomeView)");
        }
        
    }];

}


- (BOOL)shouldAutorotate {
    return NO;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];



}



@end
